package com.cloudptt.api.product;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.pjsua2.api.IPjsuaService;
import com.cloudptt.api.product.operator.CrscOperator;
import com.cloudptt.api.product.pjsua2.service.PjsuaService;
import com.cloudptt.api.product.func.SipOperator;
import com.cloudptt.api.product.func.addr.IAddrDefaultOp;
import com.cloudptt.api.product.func.addr.IAddrOp;
import com.cloudptt.api.product.func.call.CallTracker;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.api.product.operator.CallOperator;
import com.cloudptt.api.product.operator.LBSOperator;
import com.cloudptt.api.product.operator.MsgOperator;
import com.cloudptt.api.product.operator.SysMsgOperator;
import com.cloudptt.api.product.operator.SysOperator;
import com.cloudptt.api.product.operator.VoiceOperator;
import com.cloudptt.api.product.res.Resource;
import com.cloudptt.api.product.ringer.PromptTone;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.DBAdapter;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

public class ProductOperator extends SipOperator implements ISetSpeaker
{
	private static final String THIS_FILE = "ProductOperator";

	private Context ctx = null;

	private SysConfig config = null;

	protected SysOperator sysOperator = null;
	protected AddrOperator addrOperator = null;
//	protected AddrPriOperator addrPriOperator = null;
	protected ISuperCallOperator superCallOperator = null;
	protected CallOperator callOperator = null;
	protected SysMsgOperator sysMsgOperator = null;
	protected MsgOperator msgOperator = null;
	protected LBSOperator lbsOperator = null;
	protected CrscOperator crscOperator = null;
	protected CallTracker callTracker = null;
	protected VoiceOperator vrOperator = null;

	private int publishResult = 0;

	protected ProductOperator(Context ctx, SysConfig config, DBAdapter dbOperator, IAddrOp setCur, IAddrDefaultOp defaultOp)
	{
		super(ctx);
		Log.d(THIS_FILE, "ProductOperator");
		this.ctx = ctx;
		McIdInfo info = config.getMcpttLoginInfo().getMcIdInfo();

		this.sysOperator = new SysOperator(ctx,config);
		this.sysMsgOperator = new SysMsgOperator(ctx,info,dbOperator);

		this.addrOperator = new AddrOperator(ctx,dbOperator,defaultOp,info);
//		this.addrPriOperator = new AddrPriOperator(ctx,dbOperator,defaultOp,info);
		this.callOperator = new CallOperator(ctx,dbOperator);
		this.msgOperator = new MsgOperator(ctx,dbOperator);
		this.vrOperator = new VoiceOperator(ctx);
		this.lbsOperator = new LBSOperator(ctx);
		this.crscOperator = new CrscOperator(ctx,dbOperator);

		this.callTracker = new CallTracker(ctx,info,setCur,config.getAnswerModeConfig(),this.callOperator);
		this.superCallOperator = this.callTracker;

		String lanType = config.getCurLan();
		this.promptTone = new PromptTone(ctx,this,lanType);
		this.setPromptTone(promptTone);

		Intent intent = new Intent(ctx, PjsuaService.class);
		intent.setPackage(ctx.getPackageName());
		intent.putExtra(SipManager.EXTERA, "ProductOperator");

		ctx.getApplicationContext().bindService(intent,
				connection, Context.BIND_AUTO_CREATE);
//		this.startSipService();
	}

	public void setPromptTone(PromptTone promptTone) 
	{
		Log.d(THIS_FILE,  "setPromptTone");
		//		super.setUserKey(userKey);
		sysOperator.setPromptTone(promptTone);
		sysMsgOperator.setPromptTone(promptTone);
		addrOperator.setPromptTone(promptTone);
//		addrPriOperator.setPromptTone(promptTone);
		callOperator.setPromptTone(promptTone);
		msgOperator.setPromptTone(promptTone);
		vrOperator.setPromptTone(promptTone);
		lbsOperator.setPromptTone(promptTone);
		crscOperator.setPromptTone(promptTone);
	}

	protected void startSipService()
	{
		Log.d(THIS_FILE, "startSipService");
		try
		{
			Intent intent = new Intent(ctx,PjsuaService.class);
			//intent.setAction(SipManager.INTENT_SIP_SERVICE);
			intent.setPackage(ctx.getPackageName());
//			intent.addFlags(0x01000000);
			intent.putExtra(SipManager.EXTERA, "ProductOperator");
			intent.putExtra(SipManager.EXTRA_OUTGOING_ACTIVITY,
					new ComponentName(this.ctx,
							Product.class));
			this.ctx.startService(intent);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

//	protected void startSipService()
//	{
//		Log.d(THIS_FILE, "startSipService");
//		try
//		{
//			Intent intent = new Intent(ctx,PjsuaService.class);
//			intent.setAction(SipManager.INTENT_SIP_SERVICE);
//			intent.setPackage(ctx.getPackageName());
//			intent.addFlags(0x01000000);
//			intent.putExtra(SipManager.EXTERA, "ProductOperator");
//			intent.putExtra(SipManager.EXTRA_OUTGOING_ACTIVITY,
//					new ComponentName(this.ctx,
//							Product.class));
//			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
//			{
//				this.ctx.startForegroundService(intent);
//			} else {
//				this.ctx.startService(intent);
//			}
////			this.ctx.startService(intent);
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//		}
//	}

	protected  void stopService()
	{
		Log.d(THIS_FILE, "stopService");
		Intent serviceIntent = new Intent(SipManager.INTENT_SIP_SERVICE);
		this.ctx.stopService(serviceIntent);
	}


	//	public int start()
	//	{
	//		Log.d(THIS_FILE, "start");
	//		int result = checkFirst();
	//		if(CauseCont.OperateCheck_Suc != result)
	//		{
	//			return result;
	//		}
	//		try 
	//		{
	//			service.sipStart();
	//		} 
	//		catch (Exception e) 
	//		{
	//			e.printStackTrace();
	//			return CauseCont.OperateCheck_Sys_OperateException;
	//		}
	//		return CauseCont.OperateCheck_Suc;
	//	}

	public int restart()
	{
		Log.d(THIS_FILE, "restart");
		//		int result = checkFirst();
		//		if(CauseCont.OperateCheck_Suc != result)
		//		{
		//			return result;
		//		}
		try 
		{
			if(service != null) {
				service.askThreadedRestart();
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int stop()
	{
		Log.d(THIS_FILE, "stop");
		int result = checkServiceNotNull();
		if(CauseCont.OperateCheck_Suc != result)
		{
			Log.d(THIS_FILE, "checkFirst result = " + result);
			return result;
		}
		try 
		{
			service.forceStopService();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	protected IPjsuaService service;
	private ServiceConnection connection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder binder)
		{
			Log.d(THIS_FILE, "onServiceConnected");
			service = IPjsuaService.Stub.asInterface(binder);
			setService(service);
			sysOperator.setService(service);
			sysMsgOperator.setService(service);
			addrOperator.setService(service);
//			addrPriOperator.setService(service);
			callOperator.setService(service);
			msgOperator.setService(service);
			vrOperator.setService(service);
			lbsOperator.setService(service);
			crscOperator.setService(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) 
		{
			Log.d(THIS_FILE, "onServiceDisconnected");
			msgOperator.setService(null);
			sysMsgOperator.setService(null);
			callOperator.setService(null);
			addrOperator.setService(null);
//			addrPriOperator.setService(null);
			sysOperator.setService(null);
			vrOperator.setService(null);
			lbsOperator.setService(null);
			crscOperator.setService(null);
			setService(null);
			service = null;
		}
	};


		public void setPublishResult(int result)
		{
			Log.d(THIS_FILE,  "setPublishRsult result = " + result);
			this.publishResult = result;
			callOperator.setPublishResult(result);
			msgOperator.setPublishResult(result);
			crscOperator.setPublishResult(result);
		}

	public void setOperatorStatue(int status) 
	{
		Log.d(THIS_FILE,  "setOperatorStatue status = " + status);
		super.setOperatorStatue(status);
		sysOperator.setOperatorStatue(status);
		sysMsgOperator.setOperatorStatue(status);
		addrOperator.setOperatorStatue(status);
//		addrPriOperator.setOperatorStatue(status);
		callOperator.setOperatorStatue(status);
		msgOperator.setOperatorStatue(status);
		vrOperator.setOperatorStatue(status);
		lbsOperator.setOperatorStatue(status);
		crscOperator.setOperatorStatue(status);
	}

	public int getOperatorStatue()
	{
		return this.curStatus;
	}

	public void setOnLine(boolean isOnLine) 
	{
		Log.d(THIS_FILE,  "setOnLine isOnLine = " + isOnLine);
		super.setOnLine(isOnLine);
		sysOperator.setOnLine(isOnLine);
		sysMsgOperator.setOnLine(isOnLine);
		addrOperator.setOnLine(isOnLine);
//		addrPriOperator.setOnLine(isOnLine);
		callOperator.setOnLine(isOnLine);
		msgOperator.setOnLine(isOnLine);
		vrOperator.setOnLine(isOnLine);
		crscOperator.setOnLine(isOnLine);
		//lbsOperator.setOnLine(isOnLine);
	}

	public void setUpdate(boolean isUpdate) 
	{
		Log.d(THIS_FILE,  "setUpdate isUpdate = " + isUpdate);
		super.setUpdate(isUpdate);
		sysOperator.setUpdate(isUpdate);
		sysMsgOperator.setUpdate(isUpdate);
		addrOperator.setUpdate(isUpdate);
//		addrPriOperator.setUpdate(isUpdate);
		callOperator.setUpdate(isUpdate);
		msgOperator.setUpdate(isUpdate);
		vrOperator.setUpdate(isUpdate);
		lbsOperator.setUpdate(isUpdate);
		crscOperator.setUpdate(isUpdate);
	}

	public void clearResource()
	{
		this.ctx.getApplicationContext().unbindService(connection);
		super.clearResource();
		vrOperator = null;
		msgOperator= null;
		callOperator= null;
		addrOperator= null;
//		addrPriOperator = null;
		sysMsgOperator = null;
		sysOperator= null;
		lbsOperator = null;
		crscOperator = null;
		promptTone.onDestroy();
		promptTone = null;
	}

	public String getSrvStatusDscp()
	{
		Log.d(THIS_FILE,  "getSrvStatusDscp curStatus = " + curStatus);
		String key = "srv_status_dscp_";
		String srvStatusDscp = "";
		srvStatusDscp = Resource.getString(key + curStatus);
		return srvStatusDscp;
	}


	public CallOperator getCallOperator() {
		return callOperator;
	}

	public void setCallOperator(CallOperator callOperator) {
		this.callOperator = callOperator;
	}

	public VoiceOperator getVrOperator() {
		return vrOperator;
	}

	public void setVrOperator(VoiceOperator vrOperator) {
		this.vrOperator = vrOperator;
	}

	//	public boolean isLogined() {
	//		if (this.curStatus == SipOperator.Status_Srv_Logined) 
	//		{
	//			return true;
	//		}
	//		return false;
	//	}

	@Override
	public void setSpeaker(boolean isOn) {
		// TODO Auto-generated method stub
		if(service != null)
		{
			//			try {
			////				service.setSpeakerphoneOn(isOn);
			//			} catch (RemoteException e) {
			//				// TODO Auto-generated catch block
			//				e.printStackTrace();
			//			}
		}
	}
	public void onLanChange(String lanType) {
		promptTone.setLanType(lanType);
	}
}
